package com.prtek.reminder.model;

import android.content.Context;
import com.prtek.iis.core.PrtekIIS;
import com.prtek.iis.core.PrtekIISUtils;
import com.prtek.iis.event.SpeechEvent;
import com.prtek.iis.event.SpeechEventListener;
import com.prtek.iis.event.SpeechEventOptions;
import com.prtek.iis.event.SpeechEventResult;
import com.prtek.iis.grammar.Expression;
import com.prtek.iis.grammar.Expressions;
import com.prtek.iis.speech.SpeechRecognizerResult;
import com.prtek.reminder.R;
import com.prtek.reminder.bean.UserExpression;
import com.prtek.reminder.util.LogUtil;
import com.prtek.reminder.util.ToastUtil;
import java.util.List;

/**
 * Created by jarly on 2018/11/19.
 */

public class SpeechEventModelImpl implements SpeechEventModel {
  private PrtekIIS mPrtekIIS;
  private Context mContext;
  private Expression mExpression;
  private SpeechResultEditListener mResultEditListener;
  private SpeechResultMainListener mResultMainListener;
  private String[] mCommandArr;

  @Override public void setupSpeechEventListener(Context context,Expression expression) {
    mContext = context.getApplicationContext();
    mCommandArr = mContext.getResources().getStringArray(R.array.voice_command);
    mExpression = expression;
    mPrtekIIS = PrtekIISUtils.createPrtekIIS(mContext);
    final SpeechEventOptions speechResultEventOptions = new SpeechEventOptions();
    speechResultEventOptions.setAsync(false);
    speechResultEventOptions.setRunOnUiThread();
    LogUtil.d(
        "expression :" + mExpression.toString() + "\n" + mExpression.toExpandedString("\n"));
    speechResultEventOptions.setExpression(mExpression);
    speechResultEventOptions.setBackground(true);
    mPrtekIIS.addEventListener(mSpeechEventListener, SpeechEvent.TYPE_RESULT,
        speechResultEventOptions);
  }

  private SpeechEventListener mSpeechEventListener = new SpeechEventListener() {

    @Override public void onEvent(SpeechEvent event, SpeechEventResult result) {
      switch (event.getEventType()) {
        case SpeechEvent.TYPE_RESULT:
          final SpeechRecognizerResult eventResult = event.getResult();

          if (eventResult == null) {
            ToastUtil.showTip(mContext,"没有结果");
            return;
          }

          String resultText = eventResult.getText();
          ToastUtil.showTip(mContext,resultText);
          final List<Expression> expressions = Expressions.find(mExpression, resultText);

          final Expression expression = expressions.get(0);

          if (expression instanceof UserExpression) {
            mResultEditListener.addSelectUser(resultText);
            return;
          }

          if (resultText.equals(mCommandArr[0])) {
            mResultEditListener.remindConfirm();
          } else if (resultText.equals(mCommandArr[1])) {
            mResultEditListener.remindCancel();
          } else if (resultText.equals(mCommandArr[2])) {
            mResultEditListener.remindSave();
          } else if (resultText.equals(mCommandArr[3])) {
            mResultEditListener.remindDesc();
          } else if (resultText.equals(mCommandArr[4])) {
            mResultEditListener.remindDate();
          } else if (resultText.equals(mCommandArr[5])) {
            mResultEditListener.selectUser();
          } else if (resultText.equals(mCommandArr[6])) {
            mResultEditListener.remindAudio();
          } else if (resultText.equals(mContext.getString(R.string.add_msg))) {
            mResultMainListener.remindCreate();
          }
          break;
        default:
          break;
      }
    }
  };

  @Override public void removeSpeechEventListener() {
    if (mPrtekIIS!=null) {
      mPrtekIIS.removeEventListener(mSpeechEventListener);
      mPrtekIIS.release();
      mPrtekIIS = null;
    }

  }

  @Override public void setSpeechResultEditListener(SpeechResultEditListener resultEditListener) {

    mResultEditListener = resultEditListener;
  }

  @Override
  public void setSpeechResultMainListener(SpeechResultMainListener resultMainListener) {

    mResultMainListener = resultMainListener;
  }
}
